<template>
  <div class="container-s">
    <Breadcrumb :items="['个人中心', '用户设置']" />
    
    <div class="page-header">
      <h2 class="page-title">账号设置</h2>
      <p class="page-desc">您可以在这里修改个人信息、更新安全设置和管理头像</p>
    </div>
    
    <div class="settings-layout">
      <!-- 用户信息面板 -->
      <div class="panel user-panel">
        <UserPanel />
      </div>
      
      <!-- 左侧边栏 -->
      <div class="sidebar">
        <div class="avatar-upload">
          <h3 class="panel-title">头像设置</h3>
          <div class="upload-container">
            <a-upload
              :custom-request="handleUpload"
              :show-file-list="false"
              accept="image/*"
            >
              <div class="avatar-wrapper">
                <template v-if="userInfo.avatar">
                  <img :src="userInfo.avatar" class="avatar-img" />
                  <div class="avatar-overlay">
                    <icon-camera />
                    <span>更换头像</span>
                  </div>
                </template>
                <div v-else class="avatar-placeholder">
                  <icon-upload />
                  <div class="placeholder-text">上传头像</div>
                </div>
              </div>
            </a-upload>
          </div>
          <div class="upload-tips">
            <p>支持 JPG、PNG 格式，小于 2MB</p>
          </div>
        </div>
      </div>
      
      <!-- 主要内容区 -->
      <div class="main-content">
        <a-tabs default-active-key="1">
          <a-tab-pane key="1" title="基本信息">
            <BasicInformation />
          </a-tab-pane>
          <a-tab-pane key="2" title="安全设置">
            <SecuritySettings />
          </a-tab-pane>
        </a-tabs>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import UserPanel from '@/views/user/setting/components/user-panel.vue';
import BasicInformation from '@/views/user/setting/components/basic-information.vue';
import SecuritySettings from '@/views/user/setting/components/security-settings.vue';
import { ref } from 'vue';
import { Message } from '@arco-design/web-vue';
import { OjFileService } from '@/api/gen-api/services/OjFileService';
import { useUserStore } from '@/store';
import type { RequestOption } from '@arco-design/web-vue/es/upload/interfaces';
import type { UploadRequest } from '@arco-design/web-vue/es/upload/interfaces';

const userStore = useUserStore();
const userInfo = ref(userStore.userInfo);

const handleUpload = (option: RequestOption): UploadRequest => {
  const uploadRequest = {
    abort: () => {
      // 中止上传的方法
    }
  };
  
  // 在异步操作中处理文件上传
  (async () => {
    try {
      const file = option.fileItem.file as File;
      
      // 验证文件类型和大小
      if (!file.type.startsWith('image/')) {
        Message.error('请上传图片文件');
        option.onError(new Error('文件类型错误'));
        return;
      }

      if (file.size > 2 * 1024 * 1024) {
        Message.error('图片大小不能超过2MB');
        option.onError(new Error('文件过大'));
        return;
      }
      
      const formData = new FormData();
      formData.append('file', file);
      
      // 显示加载状态
      const loadingMessage = Message.loading({
        content: '正在上传头像...',
        duration: 0
      });
      
      const response = await OjFileService.uploadAvatar(formData);
      
      // 关闭加载消息
      loadingMessage.close();
      
      if (response.code === 0) {
        // 上传成功
        option.onSuccess(response.result);
        Message.success('头像上传成功');
        
        // 更新用户头像 - 使用setInfo方法更新整个用户信息对象
        userStore.setInfo({
          ...userStore.userInfo,
          avatar: response.result
        });
        
        // 更新本地显示
        userInfo.value.avatar = response.result;
      } else {
        option.onError(response);
        Message.error(response.message || '上传失败');
      }
    } catch (error: any) {
      console.error('Upload error:', error);
      option.onError(error);
      Message.error('上传失败，请稍后重试');
    }
  })();
  
  return uploadRequest;
};
</script>

<script lang="ts">
export default {
  name: 'Setting'
};
</script>

<style scoped lang="less">
.container-s {
  padding: 0 20px 40px;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin: 16px 0 24px;
  
  .page-title {
    font-size: 24px;
    font-weight: 600;
    color: var(--color-text-1);
    margin: 0 0 8px 0;
  }
  
  .page-desc {
    font-size: 14px;
    color: var(--color-text-3);
    margin: 0;
  }
}

.settings-layout {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas: 
    "panel"
    "sidebar"
    "content";
  gap: 24px;
  
  @media (min-width: 768px) {
    grid-template-columns: 280px 1fr;
    grid-template-areas: 
      "panel panel"
      "sidebar content";
  }
  
  @media (min-width: 1200px) {
    grid-template-columns: 280px 1fr;
    grid-template-areas: 
      "panel panel"
      "sidebar content";
  }
}

.panel {
  background-color: var(--color-bg-2);
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  
  &:hover {
    box-shadow: 0 4px 16px rgba(39, 89, 205, 0.1);
  }
}

.user-panel {
  grid-area: panel;
}

.sidebar {
  grid-area: sidebar;
}

.main-content {
  grid-area: content;
  
  :deep(.arco-tabs) {
    .arco-tabs-nav {
      margin-bottom: 24px;
      
      .arco-tabs-tab {
        font-weight: 500;
        font-size: 16px;
        padding: 12px 16px;
        
        &.arco-tabs-tab-active {
          color: #2759CD;
        }
      }
      
      .arco-tabs-ink-bar {
        background-color: #2759CD;
        height: 3px;
        border-radius: 3px;
      }
    }
  }
}

.avatar-upload {
  background-color: var(--color-bg-2);
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  
  .panel-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--color-text-1);
    margin: 0 0 20px 0;
  }
  
  .upload-container {
    display: flex;
    justify-content: center;
    margin-bottom: 16px;
  }
  
  .upload-tips {
    text-align: center;
    font-size: 13px;
    color: var(--color-text-3);
    
    p {
      margin: 4px 0;
    }
  }
}

.avatar-wrapper {
  position: relative;
  width: 140px;
  height: 140px;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  border: 2px dashed var(--color-border);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;

  &:hover {
    border-color: #2759CD;
    
    .avatar-overlay {
      opacity: 1;
    }
  }

  .avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  
  .avatar-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: white;
    opacity: 0;
    transition: opacity 0.3s ease;
    
    .arco-icon {
      font-size: 24px;
      margin-bottom: 8px;
    }
    
    span {
      font-size: 14px;
    }
  }

  .avatar-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    color: var(--color-text-3);

    .arco-icon {
      font-size: 28px;
      margin-bottom: 8px;
      color: #2759CD;
      opacity: 0.7;
    }

    .placeholder-text {
      font-size: 14px;
    }
  }
}

/* 响应式布局调整 */
@media (max-width: 767px) {
  .avatar-upload {
    padding: 20px;
  }
  
  .avatar-wrapper {
    width: 120px;
    height: 120px;
  }
  
  .main-content {
    :deep(.arco-tabs) {
      .arco-tabs-nav {
        .arco-tabs-tab {
          padding: 8px 12px;
          font-size: 14px;
        }
      }
    }
  }
}
</style>
